 
--Delete these unused tables  
Drop TABLE IF EXISTS "LabGroup";  
Drop TABLE IF EXISTS "LabOrderHdr";
Drop TABLE IF EXISTS "LabOrderValue";
Drop TABLE IF EXISTS "LabOrder";
 --Add a Map table for LabPackage with Location 
CREATE TABLE IF NOT EXISTS "LocationLabPackageMap" (
	"LocationLabPackageMapId"  serial PRIMARY KEY,
	"LocationId" INT  REFERENCES  "Location"("LocationId") NOT NULL,
	"LabPackageId" INT  REFERENCES  "LabPackage"("LabPackageId") NOT NULL,
	"Amount" numeric not null,
	"IsActive" bool DEFAULT true,
	"CreatedBy" INT REFERENCES  "Account"("AccountId"),
	"CreatedDate" timestamp(6) without time zone, 
    "ModifiedBy" integer REFERENCES  "Account"("AccountId"),
    "ModifiedDate" timestamp(6) without time zone
);
--Add a Map table for LocationLabHeaderMap with Location 
CREATE TABLE IF NOT EXISTS "LocationLabHeaderMap" (
	"LocationLabHeaderMapId"  serial PRIMARY KEY,
	"LocationId" INT  REFERENCES  "Location"("LocationId") NOT NULL,
	"LabHeaderId" INT  REFERENCES  "LabHeader"("LabHeaderId") NOT NULL,
	"Charge" numeric    NOT NULL,
	"InPatientCharge" numeric ,
	"IsActive" bool DEFAULT true,
	"CreatedBy" INT REFERENCES  "Account"("AccountId"),
	"CreatedDate" timestamp(6) without time zone, 
    "ModifiedBy" integer REFERENCES  "Account"("AccountId"),
    "ModifiedDate" timestamp(6) without time zone
);
  
  --Select * from "Location"
--Insert Data into  "LocationLabPackageMap" table 
INSERT INTO "LocationLabPackageMap" ("LocationId","LabPackageId","Amount")
SELECT (Select "LocationId" from "Location" where "Name"='Vikarabad'), "LabPackageId","Amount"  from "LabPackage"; 

--Insert Data into  "LocationLabHeaderMap" table 
INSERT INTO "LocationLabHeaderMap" ("LocationId","LabHeaderId","Charge","InPatientCharge")
SELECT (Select "LocationId" from "Location" where "Name"='Vikarabad'), "LabHeaderId","Charge","InPatientCharge"  from "LabHeader"; 

 

--Add LocationMapping to LabBookingHeader Table 
ALTER TABLE "LabBookingHeader" ADD COLUMN IF NOT EXISTS "LocationId"  INT REFERENCES   "Location"("LocationId"); 

--Update LocationId in "LabBookingHeader" table 
UPDATE   "LabBookingHeader" SET  "LocationId"=  (Select "LocationId" from "Location" where "Name"='Vikarabad');
 
--Add LocationMapping to LabLog Table 
ALTER TABLE "LabLog" ADD COLUMN IF NOT EXISTS "LocationId"  INT REFERENCES   "Location"("LocationId"); 

--Update LocationId in "LabLog" table 
UPDATE   "LabLog" SET  "LocationId"=  (Select "LocationId" from "Location" where "Name"='Vikarabad');
